package com.foreveross.taskservice.application.impl.pages;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import com.foreveross.taskservice.application.channel.IGradChannelApplication;
import com.foreveross.taskservice.application.impl.BaseJpaApplicationImpl;
import com.foreveross.taskservice.application.impl.convert.AirFreightChannelConvert;
import com.foreveross.taskservice.application.pages.IPageChannelApplication;
import com.foreveross.taskservice.application.task.statsdetail.vo.BaseChannelVO;
import com.foreveross.taskservice.common.model.AbstractChannelModel;
import com.foreveross.taskservice.core.domain.channel.GradChannelEntity;
import com.google.common.collect.Lists;
@Component
public  class PageChannelApplicationImpl extends BaseJpaApplicationImpl implements IPageChannelApplication{
	@Resource(name="gradChannelApplicationImpl")
	private IGradChannelApplication gradChannelApplication;

	@Override
	public List<BaseChannelVO> getAllChannels() throws Exception {
		List<GradChannelEntity> entitys=gradChannelApplication.getAllChannel();
		return buildVos(entitys);
	}

	@Override
	public List<BaseChannelVO> getAllEnableChannels() throws Exception {
		List<GradChannelEntity> entitys=gradChannelApplication.getEnableChannel();
		return buildVos(entitys);
	}
	@Override
	public BaseChannelVO getChannel(long id) throws Exception {
		GradChannelEntity entity=gradChannelApplication.get(GradChannelEntity.class, id);
		return buildVo(entity);
	}

	private List<BaseChannelVO> buildVos(List<GradChannelEntity> entitys)throws Exception{
		List<BaseChannelVO> vos=Lists.newArrayList();
		BaseChannelVO vo;
		if(CollectionUtils.isNotEmpty(entitys)){
			for(GradChannelEntity e:entitys){
				vo=buildVo(e);
				if(vo!=null){
					vos.add(vo);
				}
			}
		}
		return vos;
	}
	private BaseChannelVO buildVo(GradChannelEntity entity)throws Exception{
		BaseChannelVO  vo = null;
		if(entity!=null){
			AbstractChannelModel m=AirFreightChannelConvert.convert(entity);
			vo=new BaseChannelVO();
			vo.setChannelModel(m);
			vo.setChannelId(m.getId());
			vo.setName(m.getName());
		}
		return vo;
	}
	
	

}
